///************************************************************************
/// <copyrigth>2018-2019 Corporation.All Rights Reserved</copyrigth>
/// <author>tangyingzhong</author>
/// <contact>tangyz114987@outlook.com</contact>
/// <version>v1.0.0</version>
/// <describe>
/// It is duty for running the plugin manager and offering other services such as thread pool control
///</describe>
/// <date>2019/7/25</date>
///***********************************************************************
#ifndef MICROCORE_H
#define MICROCORE_H

#include <mutex>
#include "IMicroCore.h"
#include "IPluginManager.h"
#include "IThreadPool.h"

class MicroCore :public PluginBase<IMicroCore>
{
public:
	// Construct the kernel
	MicroCore();

	// Detructe the kernel
	virtual ~MicroCore();

private:
	// Forbid the copy kernel
	MicroCore(const MicroCore& other) {	}

	// Forbid the assigment of kernel
	MicroCore& operator=(const MicroCore& other) { return *this; }

public:
	// Startup the micro core
	virtual bool Start();

	// Shutdown the micro core
	virtual bool Stop();

	// Is core's debug mode
	virtual bool IsDebugMode();

	// Get the plugin by name and version
	virtual IPlugin* GetPlugin(std::string strPluginName,
		Int32 iMajorVersion,
		Int32 iMinorVersion,
		Int32 iModifyVersion);

	// Add Task to the micro core
	virtual bool AddTask(Task& UserTask);

	// Pause the task
	virtual void PauseTask(std::string strTaskId);

	// Resume the task
	virtual void ResumeTask(std::string strTaskId);

	// Stop the task
	virtual void StopTask(std::string strTaskId);

private:
	// Start thread pool
	bool StartThreadPool();

	// Stop the thread pool
	bool StopThreadPool();

	// Start plugin manager
	bool StartPluginManager();

	// Stop the plugin manager
	bool StopPluginManager();

	// Start main service
	bool StartMainService();

private:
	// Init the kernel 
	None Initialize();

	// Destory the kernel
	None Destory();

	// Create plugin manager
	void CreatePluginManager();

	// Destory plugin manager
	void DestoryPluginManager();

	// Create thread pool
	void CreateThreadPool();

	// Destory thread pool
	void DestoryThreadPool();

	// Configure the core
	bool MakeConfiguration();

private:
	// Get plugin manager
	inline IPluginManager* GetPluginManager() const
	{
		return m_pPluginManager;
	}

	// Set plugin manager
	inline None SetPluginManager(IPluginManager* pManager)
	{
		m_pPluginManager = pManager;
	}

	// Get thread pool
	inline IThreadPool* GetThreadPool() const
	{
		return m_pThreadPool;
	}

	// Set thread pool
	inline None SetThreadPool(IThreadPool* pPool)
	{
		m_pThreadPool = pPool;
	}

	// Get pool size
	inline Int32 GetPoolSize() const
	{
		return m_iThreadPoolSize;
	}

	// Set pool size
	inline None SetPoolSize(Int32 iSize)
	{
		m_iThreadPoolSize = iSize;
	}

	// Get configure file path
	inline String GetConfigFilePath() const
	{
		return m_strConfigFilePath;
	}

	// Set configure file path
	inline None SetConfigFilePath(String strFilePath)
	{
		m_strConfigFilePath = strFilePath;
	}

	// Get is core's debug mode
	inline Boolean GetIsDebugMode() const
	{
		return m_bIsDebugMode;
	}

	// Set is core's debug mode
	inline None SetIsDebugMode(Boolean bIsDebug)
	{
		m_bIsDebugMode = bIsDebug;
	}

	// Set the disposed status
	inline None SetDisposed(Boolean bDisposed)
	{
		m_bDisposed = bDisposed;
	}

	// Get the disposed status
	inline Boolean GetDisposed() const
	{
		return m_bDisposed;
	}

private:
	// Configuration file path
	String m_strConfigFilePath;

	// Plugin manager
	IPluginManager* m_pPluginManager;

	// Plugin manager loader
	PluginLoader<IPluginManager> m_ManagerLoader;

	// Thread pool
	IThreadPool* m_pThreadPool;

	// Thread pool loader
	PluginLoader<IThreadPool> m_PoolLoader;

	// Pool size
	Int32 m_iThreadPoolSize;
	
	// Is debug mode
	Boolean m_bIsDebugMode;

	// Disposed status
	Boolean m_bDisposed;
};

#endif // MICROCORE_H